import 'package:efood_multivendor/data/model/response/product_model.dart';
import 'package:efood_multivendor/data/model/response/restaurant_model.dart';
import 'package:efood_multivendor/helper/responsive_helper.dart';
import 'package:efood_multivendor/helper/route_helper.dart';
import 'package:efood_multivendor/helper/size_config.dart';
import 'package:efood_multivendor/util/dimensions.dart';
import 'package:efood_multivendor/view/base/no_data_screen.dart';
import 'package:efood_multivendor/view/base/product_shimmer.dart';
import 'package:efood_multivendor/view/base/product_widget.dart';
import 'package:efood_multivendor/view/screens/product/product_screen.dart';
import 'package:efood_multivendor/view/screens/restaurant/restaurant_screen.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

class ProductView extends StatelessWidget {
  final List<Product>? products;
  final List<Restaurant>? restaurants;
  final bool isRestaurant;
  final EdgeInsetsGeometry padding;
  final bool isScrollable;
  final int shimmerLength;
  final String? noDataText;
  final bool isCampaign;
  final bool inRestaurantPage;
  final String? type;
  final bool fromRestaurant;
  final bool fromSuggestion;
  final bool fromCategory;
  final Function(String type)? onVegFilterTap;
  final bool fromRestaurantSearch;

  ProductView(
      {required this.restaurants,
      required this.products,
      required this.isRestaurant,
      required this.fromRestaurant,
      this.fromSuggestion = false,
      this.fromCategory = false,
      this.isScrollable = false,
      this.shimmerLength = 20,
      this.padding = const EdgeInsets.all(Dimensions.PADDING_SIZE_SMALL),
      this.noDataText,
      this.isCampaign = false,
      this.inRestaurantPage = false,
      this.type,
      this.onVegFilterTap,
      this.fromRestaurantSearch = false});

  @override
  Widget build(BuildContext context) {
    bool _isNull = true;
    int _length = 0;
    if (isRestaurant) {
      _isNull = restaurants == null;
      if (!_isNull) {
        _length = restaurants?.length ?? 0;
      }
    } else {
      _isNull = products == null;
      if (!_isNull) {
        _length = products?.length ?? 0;
      }
    }

    return Column(children: [
      !_isNull
          ? _length > 0
              ? GridView.builder(
                  key: UniqueKey(),
                  gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                    crossAxisSpacing: Dimensions.PADDING_SIZE_LARGE,
                    mainAxisSpacing: ResponsiveHelper.isDesktop(context)
                        ? Dimensions.PADDING_SIZE_LARGE
                        : 1.h,
                    childAspectRatio:
                        ResponsiveHelper.isDesktop(context) ? 4 : 2.8,
                    crossAxisCount: ResponsiveHelper.isMobile(context) ? 1 : 2,
                  ),
                  physics: isScrollable
                      ? AlwaysScrollableScrollPhysics()
                      : NeverScrollableScrollPhysics(),
                  shrinkWrap: isScrollable ? false : true,
                  itemCount: _length + 1,
                  padding: padding,
                  itemBuilder: (context, index) {
                    if (index != _length) {
                      return InkWell(
                          onTap: () {
                            if (isRestaurant) {
                              Get.toNamed(
                                  RouteHelper.getRestaurantRoute(
                                      restaurants![index].id!),
                                  arguments: RestaurantScreen(
                                      restaurant: restaurants![index]));
                            } else {
                              Get.toNamed(
                                  RouteHelper.getProductRoute(
                                      products![index].id!),
                                  arguments: ProductScreen(
                                    product: products![index],
                                    fromCategory: fromCategory,
                                    fromSuggestion: fromSuggestion,
                                  ));
                            }
                          },
                          child: ProductWidget(
                            fromRestaurant: fromRestaurant,
                            formSuggestion: fromSuggestion,
                            formCategory: fromCategory,
                            isRestaurant: isRestaurant,
                            product: isRestaurant ? null : products?[index],
                            restaurant:
                                isRestaurant ? restaurants![index] : null,
                            index: index,
                            length: _length,
                            isCampaign: isCampaign,
                            inRestaurant: inRestaurantPage,
                          ));
                    } else {
                      return SizedBox(
                        height: 10.h,
                      );
                    }
                  },
                )
              : NoDataScreen(
                  text: noDataText != null
                      ? noDataText!
                      : isRestaurant == true
                          ? 'no_restaurant_available'.tr
                          : fromRestaurantSearch
                              ? 'search_food_or_restaurant'.tr
                              : 'no_food_available'.tr,
                )
          : GridView.builder(
              key: UniqueKey(),
              gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                crossAxisSpacing: Dimensions.PADDING_SIZE_LARGE,
                mainAxisSpacing: ResponsiveHelper.isDesktop(context)
                    ? Dimensions.PADDING_SIZE_LARGE
                    : 0.01,
                childAspectRatio: ResponsiveHelper.isDesktop(context) ? 4 : 4,
                crossAxisCount: ResponsiveHelper.isMobile(context) ? 1 : 2,
              ),
              physics: isScrollable
                  ? ClampingScrollPhysics()
                  : NeverScrollableScrollPhysics(),
              shrinkWrap: isScrollable ? false : true,
              itemCount: shimmerLength,
              padding: padding / 2,
              itemBuilder: (context, index) {
                return ProductShimmer(
                    isEnabled: _isNull,
                    isRestaurant: isRestaurant,
                    hasDivider: index != shimmerLength - 1);
              },
            ),
    ]);
  }
}
